package com.jamiexu.cn.chatwith.server;

import com.jamiexu.cn.chatwith.Imp.IConnectionListener;
import com.jamiexu.cn.chatwith.socket.ServerConnection;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class ServerController {
    ServerSocket serverSocket = null;

    @FXML
    public TextField server_port;

    @FXML
    public TextField server_max;

    @FXML
    public TextArea server_message;

    public static final ArrayList<ServerConnection> SOCKET_CLIENTS = new ArrayList<>();

    private int MAX_CONNECTION;

    @FXML
    public void server_start() {
        String portString = server_port.getText();
        if (portString.equals("") || portString.length() == 0 || portString.matches("[^\\d]+") || Integer.parseInt(portString) > 65535) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示：");
            alert.setContentText("请输入1-65535之间的端口号");
            alert.show();
            return;
        }

        String maxString = server_max.getText();
        if (maxString.equals("") || maxString.length() == 0 || maxString.matches("[^\\d]+") || Integer.parseInt(maxString) > 100) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示：");
            alert.setContentText("请输入1-100之间的最大连接数");
            alert.show();
            return;
        }

        MAX_CONNECTION = Integer.parseInt(maxString);
        new Thread(() -> {
            try {
                this.serverSocket = new ServerSocket(Integer.parseInt(portString));
                listenSocket();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }


    public void listenSocket() throws IOException {
        Socket socket;
        while ((socket = this.serverSocket.accept()) != null) {
//            System.out.println(socket.getInetAddress().getHostName());
            if (this.SOCKET_CLIENTS.size() >= this.MAX_CONNECTION) {
                break;
            }

            Socket finalSocket = socket;
            new Thread(() -> {
                new ServerConnection(finalSocket, new IConnectionListener() {
                    @Override
                    public void connected(String nickname) {
                        MAX_CONNECTION++;
                        server_message.setText(server_message.getText() + "\n[系统消息]: " + nickname + "进入聊天室");
                    }

                    @Override
                    public void disConnected(String nickname) {
                        server_message.setText(server_message.getText() + "\n[系统消息]: " + nickname + "退出聊天室");
                    }

                    @Override
                    public void acceptMessage(String sender, String message) {
                        server_message.setText(server_message.getText() + "\n[" + sender + "]: " + message);
                    }
                });
            }).start();

        }
    }

}
